<?php

/**
 * 行为分析-漏斗视图
 *
 * @author meng.deng
 */

require_once 'CommonController.php';

class Admin_ActDayDataFunnelvisitController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->_model = new Application_Model_ActDayDataFunnelvisit();
        
        $conversionModel = new Application_Model_ActDayDataConversion();
        $this->_whereByClear2 = $conversionModel -> getTestSourceIds();
        
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {

        
        //设置URL模板以及条件
        $urls = array();
        $vars = array();
        $wheres = array();

        
        $vars['startTime'] = trim($this->_request->get('startTime'));
        $vars['endTime'] = trim($this->_request->get('endTime'));
        $vars['dateType'] = trim($this->_request->get('dateType')) ;
        
        $vars['osName'] = trim($this->_request->get('osName'));
        $vars['source'] = trim($this->_request->get('source'));
        $vars['sourceId'] = trim($this->_request->get('sourceId'));
        $vars['level01'] = trim($this->_request->get('level01'));
        $vars['level02'] = trim($this->_request->get('level02'));
        $vars['level03'] = trim($this->_request->get('level03'));
        $vars['level04'] = trim($this->_request->get('level04'));
        $vars['level05'] = trim($this->_request->get('level05'));
        $vars['level06'] = trim($this->_request->get('level06'));
        $vars['level07'] = trim($this->_request->get('level07'));
        $urls = $vars;

        $productCode = Application_Model_UserLogin::getInstance()->getUserCurrentProductCode();
        $wheres[] = "`productCode` = '{$productCode}'";
        
        if ($vars['startTime'] != '') {
            $wheres[] = "`date` >= '{$vars['startTime']}' ";
        }
        if ($vars['endTime'] != '') {
            $wheres[] = "`date` <= '{$vars['endTime']}' ";
        }
        if ($vars['osName'] != '') {
            $wheres[] = "`osName` = '{$vars['osName']}' ";
        }
        if ($vars['source'] != '') {
            $wheres[] = "`sourceId` IN (SELECT  ms.`sourceId` FROM `mob_source` AS ms INNER JOIN `mob_member` AS mm ON  ms.memberCode=mm.code   WHERE   mm.name='{$vars['source']}' AND ms.productCode='{$productCode}') ";
        }
        if ($vars['sourceId'] != '') {
            $wheres[] = "`sourceId` = '{$vars['sourceId']}' ";
        }
        
        $wheres[] ="`date`>='2011-09-24'";
        $orderBy = "`uv` DESC";
        
        //清理测试数据
        $wheres[] = $this->_whereByClear2;

        if($_POST){
            //获取总记录
            $actPage = new Application_Model_ActPage();
            $where = count($wheres) > 0 ? ' WHERE ' . implode(' AND ', $wheres) . ' ' : '';
            /*
            //首页uv [算来源]
            $sql = "SELECT pageId,pageName,sum(uv) as uv FROM `{$this->_model->getTableName()}`  {$where} AND `pageId`='{$vars['level01']}'  GROUP BY pageId,pageName ORDER BY {$orderBy}";
            $rows1 = $this ->_model ->getAdapter()->fetchAll($sql);
            */
            //首页uv [不算来源]
            $pagevisitMode = new Application_Model_ActDayDataPagevisit();
            $sql = "SELECT pageId,pageName,sum(uv) as uv FROM `{$pagevisitMode->getTableName()}`  {$where} AND `pageId`='{$vars['level01']}'  GROUP BY pageId,pageName ORDER BY {$orderBy}";
            $rows1 = $pagevisitMode ->getAdapter()->fetchAll($sql);
            if(count($rows1)>0){
                $rows1[0]['eventPageId']=0;//设置到首页的所有来源页
            }
            $sql = "SELECT eventPageId,pageId,pageName,sum(uv) as uv FROM `{$this->_model->getTableName()}`  {$where} AND `parentIds`='{$vars['level01']}' AND `pageId`='{$vars['level02']}'  GROUP BY eventPageId,pageId,pageName ORDER BY {$orderBy} limit 1";
            $rows2 = $this ->_model ->getAdapter()->fetchAll($sql);
            
            if(!$rows1){
                $pageName = $actPage ->pageNameById($vars['level01']);
                $rows1 = array(array('eventPageId'=>0,'pageId'=>$vars['level01'],'pageName'=>$pageName,'uv'=>0));
            }
            if(!$rows2){
                $pageName = $actPage ->pageNameById($vars['level02']);
                $rows2 = array(array('eventPageId'=>$vars['level01'],'pageId'=>$vars['level02'],'pageName'=>$pageName,'uv'=>0));
            }
            if($vars['level02']=='1020'){//普通专题不走商品列表走购物车
                $rows3 = array();
                $sql = "SELECT eventPageId,pageId,pageName,sum(uv) as uv FROM `{$this->_model->getTableName()}`  {$where} AND `parentIds`='{$vars['level02']}' AND `pageId`='{$vars['level04']}'  GROUP BY eventPageId,pageId,pageName ORDER BY {$orderBy} limit 1";
                $rows4 = $this ->_model ->getAdapter()->fetchAll($sql);
                $sql = "SELECT pageId,pageName,sum(uv) as uv FROM `{$this->_model->getTableName()}`  {$where} AND `parentIds`='{$vars['level02']}' AND `pageId`='{$vars['level05']}'  GROUP BY pageId,pageName ORDER BY {$orderBy} limit 1";
                $rows5 = $this ->_model ->getAdapter()->fetchAll($sql);
                //基于订单中专题的Id来得到数据
                /*$sql = "SELECT pageId,pageName,sum(uv) as uv FROM `{$this->_model->getTableName()}`  {$where} AND `parentIds`='{$vars['level02']}' AND `pageId`='{$vars['level07']}'  GROUP BY pageId,pageName ORDER BY {$orderBy} limit 1";
                $rows7 = $this ->_model ->getAdapter()->fetchAll($sql);*/
                //基于时间倒序比对从专题 过来的订单
                $topictoorderModel = new Application_Model_ActDayDataTopictoorder();
                $sql = "SELECT eventPageId,pageId,sum(uv) as uv FROM `{$topictoorderModel->getTableName()}` {$where} AND `eventPageId`='{$vars['level02']}' AND `pageId`='{$vars['level07']}' GROUP BY eventPageId,pageId ORDER BY {$orderBy} limit 1";
                $topictoorderRows = $topictoorderModel ->getAdapter()->fetchAll($sql);
                $rows7 = array();
                if ($topictoorderRows){
                    $rows7 = array(array('eventPageId'=>$topictoorderRows[0]['eventPageId'],'pageId'=>$topictoorderRows[0]['pageId'],'pageName'=>'订单','uv'=>$topictoorderRows[0]['uv']));
                }
                
                
                
                if(!$rows4){
                    $pageName = $actPage ->pageNameById($vars['level04']);
                    $rows4 = array(array('eventPageId'=>$vars['level02'],'pageId'=>$vars['level04'],'pageName'=>$pageName,'uv'=>0));
                }
                if(!$rows5){
                    $pageName = $actPage ->pageNameById($vars['level05']);
                    $rows5 = array(array('eventPageId'=>$vars['level04'],'pageId'=>$vars['level05'],'pageName'=>$pageName,'uv'=>0));
                }
                if(!$rows7){
                    $pageName = $actPage ->pageNameById($vars['level07']);
                    $rows7 = array(array('eventPageId'=>$vars['level02'],'pageId'=>$vars['level07'],'pageName'=>$pageName,'uv'=>0));
                }
            } elseif ($vars['level02']=='1022' || $vars['level02']=='1023'){//特殊专题[限时抢购]不走商品列表不走购物车
                $rows3 = array();
                $sql = "SELECT eventPageId,pageId,pageName,sum(uv) as uv FROM `{$this->_model->getTableName()}`  {$where} AND `parentIds`='{$vars['level02']}' AND `pageId`='{$vars['level04']}'  GROUP BY eventPageId,pageId,pageName ORDER BY {$orderBy} limit 1";
                $rows4 = $this ->_model ->getAdapter()->fetchAll($sql);
                $rows5 = array();
                //基于订单中专题的Id来得到数据
                /*$sql = "SELECT pageId,pageName,sum(uv) as uv FROM `{$this->_model->getTableName()}`  {$where} AND `parentIds`='{$vars['level02']}' AND `pageId`='{$vars['level07']}'  GROUP BY pageId,pageName ORDER BY {$orderBy} limit 1";
                $rows7 = $this ->_model ->getAdapter()->fetchAll($sql);*/
                
                //基于时间倒序比对从专题 过来的订单
                $topictoorderModel = new Application_Model_ActDayDataTopictoorder();
                $sql = "SELECT eventPageId,pageId,sum(uv) as uv FROM `{$topictoorderModel->getTableName()}` {$where} AND `eventPageId`='{$vars['level02']}' AND `pageId`='{$vars['level07']}' GROUP BY eventPageId,pageId ORDER BY {$orderBy} limit 1";
                $topictoorderRows = $topictoorderModel ->getAdapter()->fetchAll($sql);
                $rows7 = array();
                if ($topictoorderRows){
                    $rows7 = array(array('eventPageId'=>$topictoorderRows[0]['eventPageId'],'pageId'=>$topictoorderRows[0]['pageId'],'pageName'=>'订单','uv'=>$topictoorderRows[0]['uv']));
                }
                
                
                if(!$rows4){
                    $pageName = $actPage ->pageNameById($vars['level04']);
                    $rows4 = array(array('eventPageId'=>$vars['level02'],'pageId'=>$vars['level04'],'pageName'=>$pageName,'uv'=>0));
                }
                if(!$rows7){
                    $pageName = $actPage ->pageNameById($vars['level07']);
                    $rows7 = array(array('eventPageId'=>$vars['level02'],'pageId'=>$vars['level07'],'pageName'=>$pageName,'uv'=>0));
                }
            } else{
                $sql = "SELECT eventPageId,pageId,pageName,sum(uv) as uv FROM `{$this->_model->getTableName()}`  {$where} AND `parentIds`='{$vars['level02']}' AND `pageId`='{$vars['level03']}'  GROUP BY eventPageId,pageId,pageName ORDER BY {$orderBy} limit 1";
                $rows3 = $this ->_model ->getAdapter()->fetchAll($sql);
                $sql = "SELECT eventPageId,pageId,pageName,sum(uv) as uv FROM `{$this->_model->getTableName()}`  {$where} AND `parentIds`='{$vars['level03']}' AND `pageId`='{$vars['level04']}'  GROUP BY eventPageId,pageId,pageName ORDER BY {$orderBy} limit 1";
                $rows4 = $this ->_model ->getAdapter()->fetchAll($sql);
                $sql = "SELECT pageId,pageName,sum(uv) as uv FROM `{$this->_model->getTableName()}`  {$where} AND (`parentIds`='{$vars['level02']}' OR `parentIds`='{$vars['level03']}') AND `pageId`='{$vars['level05']}'  GROUP BY pageId,pageName ORDER BY {$orderBy} limit 1";
                $rows5 = $this ->_model ->getAdapter()->fetchAll($sql);
                $sql = "SELECT pageId,pageName,sum(uv) as uv FROM `{$this->_model->getTableName()}`  {$where} AND (`parentIds`='{$vars['level02']}' OR `parentIds`='{$vars['level03']}') AND `pageId`='{$vars['level07']}'  GROUP BY pageId,pageName ORDER BY {$orderBy} limit 1";
                $rows7 = $this ->_model ->getAdapter()->fetchAll($sql);
                
                
                if(!$rows3){
                    $pageName = $actPage ->pageNameById($vars['level03']);
                    $rows3 = array(array('eventPageId'=>$vars['level02'],'pageId'=>$vars['level03'],'pageName'=>$pageName,'uv'=>0));
                }
                if(!$rows4){
                    $pageName = $actPage ->pageNameById($vars['level04']);
                    $rows4 = array(array('eventPageId'=>$vars['level03'],'pageId'=>$vars['level04'],'pageName'=>$pageName,'uv'=>0));
                }
                if(!$rows5){
                    $pageName = $actPage ->pageNameById($vars['level05']);
                    $rows5 = array(array('eventPageId'=>$vars['level04'],'pageId'=>$vars['level05'],'pageName'=>$pageName,'uv'=>0));
                }
                if(!$rows7){
                    $pageName = $actPage ->pageNameById($vars['level07']);
                    $rows7 = array(array('eventPageId'=>$vars['level02'],'pageId'=>$vars['level07'],'pageName'=>$pageName,'uv'=>0));
                }
                
                
                
                
            }
            
            
            $rows = array_merge($rows1,$rows2,$rows3,$rows4,$rows5,$rows7);
            $this->view->rows = $rows;
            $this->view->vars = $vars;
        }
         
        
        
        
        //列出统计页
        $actPage = new Application_Model_ActPage();
        for($i=1;$i<=7;$i++){
            $pageRows = $this ->_model ->getAdapter()->fetchAll("SELECT * FROM `{$actPage->getTableName()}` WHERE FIND_IN_SET({$i},level) ORDER BY `id`");
            if($i==1){
               $this->view->pageRows1 = $pageRows;
            }
            if($i==2){
               $this->view->pageRows2 = $pageRows;
            }
            if($i==3){
               $this->view->pageRows3 = $pageRows;
            }
            if($i==4){
               $this->view->pageRows4 = $pageRows;
            }
            if($i==5){
               $this->view->pageRows5 = $pageRows;
            }
            if($i==6){
               $this->view->pageRows6 = $pageRows;
            }
            if($i==7){
               $this->view->pageRows7 = $pageRows;
            }
        }
        
    }

    
    /**
     * 获取APP版本号
     * @return json string
     */
    public function appversionAction() 
    {
        $osName = trim($this->_request->getPost('osName'));
        $rows = $this ->_model ->getAdapter()->fetchAll("SELECT `version` FROM `{$this->_model->getTableName()}` WHERE `osNameFlag`='".$osName."' GROUP BY `version` ORDER BY `version`");
        $res = array();
        foreach($rows as $row) {
            $res[]=$row['version'];
        }
        $json_string=json_encode($res);
        echo $json_string;
        exit();
        
    }
}